package course.d1229;

public class test1 {
    public static void main(String[] args) {
     Dumplings dumplings = new Dumplings();
     new Customers(dumplings).start();
     new DumplingsStore(dumplings).start();
    }
}
class Dumplings{
    private String pi;
    private String xian;
    private boolean isExist;

    public String getPi() {
        return pi;
    }

    public void setPi(String pi) {
        this.pi = pi;
    }

    public String getXian() {
        return xian;
    }

    public void setXian(String xian) {
        this.xian = xian;
    }

    public boolean isExist() {
        return isExist;
    }

    public void setExist(boolean exist) {
        isExist = exist;
    }
}
class Customers extends  Thread{
    private Dumplings dumplings;

    public Customers(Dumplings dumplings) {
        this.dumplings = dumplings;
    }

    @Override
    public void run() {
        int number =1;
        while (true){
            synchronized (dumplings){
                if (!(dumplings.isExist())){
                    try {
                        dumplings.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                System.out.println("顾客开始吃第  "+number+"个饺子， 吃的是"+dumplings.getPi()+dumplings.getXian()+"的饺子");
                number++;
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("通知老板做饺子");
                System.out.println("------------------\n");
                dumplings.setExist(false);
                dumplings.notify();
            }
        }

    }
}
class DumplingsStore extends Thread{
    private Dumplings dumplings;

    public DumplingsStore(Dumplings dumplings) {
        this.dumplings = dumplings;
    }

    @Override
    public void run() {
        int count =0;
        while (true){
            synchronized (dumplings){
                if (dumplings.isExist()){
                    try {
                        dumplings.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                System.out.println("老板开始做饺子");
                if (count%2==1){
                    dumplings.setPi("皮薄");
                    dumplings.setXian("猪肉馅");
                }else {
                    dumplings.setPi("皮薄");
                    dumplings.setXian("韭菜馅");
                }
                count++;
                dumplings.setExist(true);
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                dumplings.notify();
            }
        }

    }
}
